<!-- src/views/RentalStep1.vue -->
<template>
  <div class="rental-form">
    <!-- 顶部导航 -->
    <div class="nav-header">
      <div class="back">
        <i class="el-icon-arrow-left" @click="goBack"></i>
        <span>我要出租</span>
      </div>
      <div class="icons">
        <el-button type="text" icon="el-icon-more"></el-button>
        <el-button type="text" icon="el-icon-s-help"></el-button>
      </div>
    </div>

    <!-- 进度指示器 -->
    <div class="step-indicator">
      <div class="step active">1</div>
      <div class="line"></div>
      <div class="step">2</div>
      <div class="line"></div>
      <div class="step">3</div>
    </div>

    <!-- 表单内容 -->
    <div class="form-content">
      <!-- 位置选择 -->
      <div class="form-item">
        <div class="form-title">你好，这里有房子要出租~###模拟定位###</div>
        <div class="select-group">
          <el-input v-model="formData.longitude" placeholder="请输入经度"></el-input>
          <el-input v-model="formData.latitude" placeholder="请输入纬度"></el-input>
          <el-input v-model="formData.location" placeholder="请输入具体地址"></el-input>
        </div>

      </div>
      <!-- 图片上传 -->
      <div class="form-item">
        <div class="form-title">这里有房子的照片(最多可上传9张)</div>
        <div class="form-content">
          <el-upload
              action="http://127.0.0.1:9701/web/cos/upload?path=image"
              list-type="picture-card"
              :on-success="handleImageSuccess"
              :before-upload="beforeImageUpload"
              :on-preview="handlePictureCardPreview"
              :file-list="fileList"
              :on-remove="handleRemove">
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="imageShow">
            <img width="100%" :src="dialogImageUrl" alt="">
          </el-dialog>
        </div>
      </div>
      <!-- 上传房屋视频 -->
      <div class="form-item">
        <div class="form-title">这里有房子的视频可以提高成交率哦</div>
        <div class="form-content">
          <el-upload
              class="avatar-uploader"
              action="http://127.0.0.1:9701/web/cos/upload?path=video"
              :show-file-list="false"
              :on-success="handleVideoSuccess"
              :before-upload="beforeVideoUpload">
            <video v-if="videoUrl" :src="videoUrl" class="avatar"/>
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </div>
      </div>
      <!-- 附近地铁站 -->
      <div class="form-item">
        <div class="form-title">附近地铁站</div>
        <div class="select-group">
          <el-select v-model="line" placeholder="地铁线路">
            <el-option value="1号线"></el-option>
            <el-option value="2号线"></el-option>
          </el-select>
          <el-select v-model="station" placeholder="郑州火车站">
            <el-option value="郑州火车站"></el-option>
            <el-option value="郑州东站"></el-option>
          </el-select>
        </div>
      </div>
      <!-- 房型选择 -->
      <div class="form-item">
        <div class="form-title">要出租的房子是这样的</div>
        <div class="select-group">
          <el-select v-model="roomType" placeholder="一居室">
            <el-option value="一居室"></el-option>
            <el-option value="二居室"></el-option>
            <el-option value="三居室"></el-option>
            <el-option value="开间"></el-option>
            <el-option value="LOFT"></el-option>
            <el-option value="复式"></el-option>
          </el-select>
          <el-select v-model="rentType" placeholder="整租">
            <el-option value="整租"></el-option>
            <el-option value="主卧"></el-option>
            <el-option value="次卧"></el-option>
            <el-option value="创维"></el-option>
          </el-select>
          <el-select v-model="balcony" placeholder="带阳台">
            <el-option value="带阳台"></el-option>
            <el-option value="带独立卫生间"></el-option>
            <el-option value="无隔断"></el-option>
            <el-option value="有隔断"></el-option>
          </el-select>
        </div>
      </div>
      <!-- 环境要求 -->
      <div class="form-item">
        <div class="form-title">房子的居住情况与优点，希望你能喜欢</div>
        <el-input
            type="textarea"
            v-model="formData.introduce"
            placeholder="房子的装修情况，周边商场及主要功能区，到期后是否可以续租等"
            :rows="3"
        ></el-input>
      </div>
      <!-- 租金信息 -->
      <div class="form-item">
        <div class="form-title">我的月租预算、租期和租赁方式是这样的</div>
        <div class="select-group">
          <el-input v-model.number="formData.rent" placeholder="租金">
            <template slot="append">元/月</template>
          </el-input>
          <el-select v-model="formData.tenancyTerm" placeholder="3个月">
            <el-option label="3个月" :value="3"></el-option>
            <el-option label="6个月" :value="6"></el-option>
            <el-option label="12个月" :value="12"></el-option>
            <el-option label="两年" :value="24"></el-option>
            <el-option label="其他" :value="0"></el-option>
          </el-select>
          <el-select v-model="formData.payType" placeholder="押一付一">
            <el-option label="押一付一" value="押一付一"></el-option>
            <el-option label="押一付三" value="押一付三"></el-option>
            <el-option label="押一付六" value="押一付六"></el-option>
            <el-option label="押一付十二" value="押一付十二"></el-option>
          </el-select>
        </div>
      </div>

      <el-button type="primary" class="next-btn" @click="handleNext">下一步</el-button>
    </div>
  </div>
</template>

<script>
export default {
  name: 'RentalForm1',
  data() {
    return {
      fileList: [],
      roomType: '一居室',
      rentType: '整租',
      balcony: '带阳台',
      line: '1号线',
      station: '郑州火车站',
      imagesUrl: [],
      videoUrl: '',
      dialogImageUrl: '',
      imageShow: false,
      formData: {
        longitude: '',
        latitude: '',
        location: '',
        imagesUrl: '',
        videoUrl: '',
        nearbyStation: '',
        structure: '',
        introduce: '',
        rent: '',
        tenancyTerm: '',
        payType: ''
      }
    }
  },
  created() {
    // 数据回显
    if (this.$store && this.$store.state.formData) {
      const savedData = this.$store.state.formData.rental1
      console.log(savedData)
      if (savedData) {
        this.formData = { ...savedData }
      }
      if (this.formData.imagesUrl) {
        this.imagesUrl = this.formData.imagesUrl.split(',')
        let len = this.imagesUrl.length
        for (let i = 0; i < len; i++) {
          this.fileList.push({
            name: "file" + i,
            url: this.imagesUrl[i]
          })
        }
      }
      if (this.formData.nearbyStation) {
        let arr = this.formData.nearbyStation.split(',')
        this.line = arr[0]
        this.station = arr[1]
      }
      if (this.formData.structure) {
        let arr = this.formData.structure.split(',')
        this.roomType = arr[0]
        this.rentType = arr[1]
        this.balcony = arr[2]
      }
    }
  },
  methods: {
    // 上传房屋视频
    handleVideoSuccess(res, file) {
      this.videoUrl = URL.createObjectURL(file.raw);
      this.formData.videoUrl = res.data
    },
    beforeVideoUpload(file) {
      /**
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
          **/
    },
    // 图片上传
    beforeImageUpload() {
      if (this.imagesUrl.length >= 9) {
        this.$message.error('最多只能上传9张图片');
        return false;
      }
    },
    handleImageSuccess(response, file) {
      this.imagesUrl.push(response)
      this.fileList.push({
        name: file.name,
        url: response.data
      })
    },
    handleRemove(file, fileList) {
      // let url = file.response
      // let i = this.imagesUrl.indexOf(url)
      // if (i !== -1) {
      //   this.imagesUrl.splice(i, 1)
      // }
      this.fileList = fileList
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.imageShow = true;
    },
    goBack(){
      this.$router.push('/release')
    },
    handleNext() {
      try {
        // 保存数据到 Vuex
        if (this.$store) {
          this.formData.imagesUrl = this.fileList.map(file => file.url).join(',')
          this.formData.nearbyStation = this.line + ',' + this.station
          this.formData.structure = this.roomType + ',' + this.rentType + ',' + this.balcony
          this.$store.commit('SET_RENTAL1_DATA', this.formData)
        }
        // 跳转到第二步
        this.$router.push('/rentalForm2')
      } catch (error) {
        console.error('Error in handleNext:', error)
      }
    }
  }
}
</script>

<style scoped>
.icons {
  display: flex;
  border: 1px solid white;
  padding: 5px 5px;
  border-radius: 15px;
}

.icons button {
  color: white;
  padding: 0 5px;
  font-size: 18px;
}

.rental-form {
  max-width: 414px;
  margin: 0 auto;
  background: #f5f5f5;
}

.nav-header {
  background: #ff7f50;
  color: white;
  padding: 10px 15px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.step-indicator {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px 0;
}

.step {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #ddd;
}

.step.active {
  background: #409EFF;
  color: white;
}

.line {
  width: 100px;
  height: 1px;
  background: #ddd;
  margin: 0 10px;
}

.form-content {
  padding: 15px;
}

.form-item {
  background: white;
  padding: 15px;
  margin-bottom: 10px;
  border-radius: 8px;
}

.form-title {
  margin-bottom: 10px;
  color: #333;
}

.select-group {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.rent-group {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.next-btn {
  width: 100%;
  margin-top: 20px;
}

:deep(.el-select) {
  width: 100%;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
